<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 240px;
            height: 240px;
            background-color: #ddd;
            position: fixed;
            right: 0;
            bottom: 0;
        }

        span {
            float: right;
            background-color: #f00;
            padding: 10px;
            color: #fff;

            cursor: pointer;
        }
    </style>
</head>

<body>

    <div>
        <span>X</span>
        <p>10s之后自动关闭</p>
    </div>


    <script>


        // 点X关闭    盒子隐藏
        var oSpan = document.querySelector('span');
        var oBox = document.querySelector('div');
        var oP = document.querySelector('p');

        // 10s倒计时   盒子隐藏 
        var count = 10;


        // 10s倒计时
        var t = setInterval(f, 500);


        oSpan.onclick = function () {
            // 关闭小广告
            oBox.style.display = 'none';
            // 清除定时器
            clearInterval(t);
            // 10s之后又回来了
            setTimeout(function () {
                count = 10;   // 重新变成10
                oBox.style.display = 'block';

                // 定时器有1s的延迟  -- - 
                oP.innerHTML = count + 's之后自动关闭';
                // t 必须是全局变量   
                t = setInterval(f, 500);

            }, 3000)
        }





        // 实现倒计时
        function f() {
            count--;
            oP.innerHTML = count + 's之后自动关闭';
            // 判断 0
            if (count < 0) {
                oBox.style.display = 'none';
                // 清除定时器
                clearInterval(t);

                // 10s之后又回来了
                setTimeout(function () {
                    count = 10;
                    oBox.style.display = 'block';
                    oP.innerHTML = count + 's之后自动关闭';
                    t = setInterval(f, 500);
                }, 3000)

            }

        }





    </script>

</body>

</html>